﻿// Copyright (c) Inossem. All rights reserved.
// Licensed under the LGPL-3.0 license. See LICENSE file in the project root for full license information.
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DWF.Studio.RemoteCtrls.SnippetExplorer.Node
{
    internal class SnippetManager
    {
        private static readonly Uri _defaultIcon = new Uri("..\\Resources\\Snippet\\default.png", UriKind.Relative);

        public IEnumerable<SnippetNode> LoadSnippets(string path)
        {
            var dir = new System.IO.DirectoryInfo(path);
            var nodes = buildSnippetFolder(dir);
            var top = nodes.FirstOrDefault(o => o.Name == "Favorites");//收藏列
            if (top != null)
            {
                List<SnippetNode> snippetNodes = new List<SnippetNode>();
                snippetNodes.Add(top);
                var node = nodes.Where(o => o.Name != "Favorites");
                foreach (var item in node)
                {
                    snippetNodes.Add(item);
                }
                return snippetNodes;
            }
            return nodes;
        }

        private IEnumerable<SnippetNode> buildSnippetFolder(DirectoryInfo parent)
        {
            var nodes = new List<SnippetNode>();
            System.IO.DirectoryInfo[] directories = parent.GetDirectories();
            foreach (System.IO.DirectoryInfo directory in directories)
            {
                var snippetFolder = new SnippetFolder()
                {
                    Name = directory.Name,
                    FullName = directory.FullName,
                    IconLoc = _defaultIcon,
                    DisplayName = directory.Name,
                    Children = new ObservableCollection<SnippetNode>(),
                    Visible = true,
                    IsExpanded = true
                };
                var snippetFiles = buildSnippetFolder(directory);
                foreach (var snippetFile in snippetFiles)
                {
                    snippetFolder.Children.Add(snippetFile);
                }
                nodes.Add(snippetFolder);
            }
            nodes.AddRange(buildSnippetFile(parent));
            return nodes;
        }

        private IEnumerable<SnippetNode> buildSnippetFile(DirectoryInfo directory)
        {
            var nodes = new List<SnippetNode>();
            foreach (FileInfo fileInfo in directory.GetFiles("*.xaml"))
            {
                var snippetFile = new SnippetFile()
                {
                    Name = fileInfo.Name,
                    FullName = fileInfo.FullName,
                    IconLoc = _defaultIcon,
                    DisplayName = System.IO.Path.GetFileNameWithoutExtension(fileInfo.Name),
                    Visible = true,
                    IsLeaf = true,
                    IsExpanded = true
                };

                nodes.Add(snippetFile);
            }
            return nodes;
        }
    }
}
